<?php

namespace app\common\widget;

use think\Db;

class Form extends \think\Controller
{

    private $widgetPath;

    /**
     * 初始化
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->widgetPath = APP_PATH . 'common/widget/form/';
    }

    /**
     * 富文本控件
     */
    public function ueditor($field, $value = '', $uid = 0)
    {
        $parse = <<<HTML
<script id="{$field}" name="{$field}" type="text/plain">{$value}</script>
<script>
In('ueditor',function(){
    var {$field}Editor = UE.getEditor('{$field}',{
        serverUrl: '/plus/uploader/index.html?uid={$uid}'
    });
})
</script>
HTML;
        return $parse;
    }

    /**
     * 单图上传控件
     */
    public function singleImg($field, $value = '', $uid = 0)
    {
        $ucField = ucfirst(preg_replace('/[^0-9a-zA-Z]+/', '', $field)); //防止字段命名不符合规范
        $this->assign('ucField', $ucField);
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('uid', $uid);
        return $this->fetch($this->widgetPath . 'singleImg.html');
    }

    /**
     * 多图上传控件
     */
    public function moreImg($field, $value = '', $uid = 0)
    {
        if (!empty($value)) {
            $imgList = explode('|', $value);
        }
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('imgList', $imgList);
        $this->assign('uid', $uid);
        return $this->fetch($this->widgetPath . 'moreImg.html');
    }

    /**
     * 多图上传控件,包含标题和描述
     */
    public function moreTextImg($field, $value = '', $tips = '请输入图片标题', $uid = 0)
    {
        $arr = [];
        if (!empty($value) && $value !== '######') {
            $list = explode('###', $value);
            $imgList = explode('|', $list[0]); //图片
            $titleList = explode('|', $list[1]); //标题
            $infoList = explode('|', $list[2]); //描述
            //图片、文字、备注
            foreach ($imgList as $k => $v) {
                $arr[$k]['imgurl'] = $v;
                $arr[$k]['title'] = $titleList[$k];
                $arr[$k]['info'] = $infoList[$k];
            }
        }
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('imgList', $arr);
        $this->assign('uid', $uid);
        $this->assign('tips', $tips);
        return $this->fetch($this->widgetPath . 'moreTextImg.html');
    }

    /**
     * 附件上传控件
     */
    public function singleFile($field, $value = '', $uid = 0)
    {
        $ucField = ucfirst(preg_replace('/[^0-9a-zA-Z]+/', '', $field)); //防止字段命名不符合规范
        $this->assign('ucField', $ucField);
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('uid', $uid);
        return $this->fetch($this->widgetPath . 'singleFile.html');
    }

    /**
     * 多附件上传控件
     */
    public function moreFile($field, $value = '', $uid = 0)
    {
        $fileList = [];
        if (!empty($value)) {
            $values = explode('|', $value);
            foreach ($values as $k => $v) {
                $f = explode('###', $v);
                $fileList[$k]['url'] = $f[0];
                $fileList[$k]['name'] = $f[1];
            }
        }
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('fileList', $fileList);
        $this->assign('uid', $uid);
        return $this->fetch($this->widgetPath . 'moreFile.html');
    }

    /**
     * 扩展属性控件
     * @param $field
     * @param $value
     * @return mixed
     */
    public function extend($field, $value = [], $custom='')
    {
        $value = empty($value) ? [] : $value;
        $extendStr = '';
        foreach ($value as $k => $v) {
            $extendStr .= "{$k}:{$v}\r\n";
        }
        $this->assign('field', $field);
        $this->assign('extendStr', $extendStr);
        $content = $this->fetch($this->widgetPath . 'extend.html');
        return $content;
    }

    /**
     * 选择框
     * @param $field
     * @param string $value
     * @param array $custom
     * @return mixed
     */
    public function custombox($field, $value = '', $custom='')
    {
        //widget|common/Form/custombox|select:dict:dict_id,dict_name:type_id=103 and id>=38:===请选择=== //|隔开的数组
        $custom = explode('|',$custom);
        $config = explode(':', $custom[2]);
        $nodata = $config[4];
        $fields = explode(',', $config[2]);
        $query = [];
        if (count($config == 5)) { //有查询条件
            $query = stripslashes(htmlspecialchars_decode($config[3]));
        }
        if(empty($value)){
            if($this->request->has($field)){
                $value=$this->request->param($field);
            }
        }
        $select = Db::table($config[1])->field("{$fields[0]} as skey,{$fields[1]} as svalue")->where($query)->select();
        if ($select->isEmpty()) {
            $select = [];
        }
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('select', $select);
        $this->assign('nodata', $nodata);
        $content = $this->fetch($this->widgetPath . $config[0] . '.html');
        return $content;
    }

    /**
     * 用户选择控件
     * @param $field
     * @param string $value
     */
    public function selectpage($field,$value='',$custom=''){
        //要格式化formatitem时请使用formatfun作为键替换formatitem
        //'keyField:id|showField:path|searchField:path|multiple:true|maxSelectLimit:2|dbTable:area|orderBy:"id desc","name"'
        $custom = explode('|',$custom);
        $conf=[];
        $customsearch=false;
        foreach($custom as $v){
            $arr=explode(':',$v);
            //转为bool
            if(in_array($arr[0],['multiple','dropButton','pagination','multipleControlbar','selectToCloseList'])){
                $arr[1]=$arr[1]=='true'?true:false;
            }
            //转为数值
            if(in_array($arr[0],['listSize','maxSelectLimit','pageSize'])){
                $arr[1]=(int)$arr[1];
            }
            $conf[$arr[0]]=$arr[1];
            if($arr[0]=='orderBy'){
                $conf['orderBy']=explode(',',$arr[1]);
            }
            if($arr[0]=='searchField'){
                $customsearch=true;
            }
        }
        $conf['searchField'] = $customsearch===true?$conf['searchField']:$conf['showField'];
        $formatfun='';
        if(array_key_exists('formatfun',$conf)){
            $formatfun=$conf['formatfun'];
        }
        $this->assign('formatfun',$formatfun);
        $this->assign('custom',json_encode($conf));
        $this->assign('field',$field);
        $this->assign('value',$value);
        $content = $this->fetch($this->widgetPath . 'selectpage.html');
        return $content;
    }

}